home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / CLNEW.CPP < prev    next >
C/C++ Source or Header  |  1995-09-14  |  7KB  |  230 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    clnew.cpp
  5. //   Title:    C++ Class Libraries
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code to override the global new and delete operators.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <class.hpp>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //    Globals
  45. //----------------------------------------------------------------------------
  46. typedef void (* pvf)();
  47. static pvf _new_handler;
  48.  
  49. //----------------------------------------------------------------------------
  50. //    Un-define the 'new' operator so it can be redefined.
  51. //----------------------------------------------------------------------------
  52. #ifdef new
  53. #undef new
  54. #endif
  55.  
  56.  
  57. //  will undefine when compiling under MSVC...
  58.  
  59. #ifndef COMPILER_MSC
  60.  
  61. //----------------------------------------------------------------------------
  62. //   Description:    Replacement 'delete' operator.
  63. //    Parameters:    size        Size of buffer to allocate.
  64. //       Globals:    None modified.
  65. //       Returns:    TRUE if successful.
  66. //----------------------------------------------------------------------------
  67. #if COMPILE_DLL
  68. void _EXPORT_ _LOADDS_ operator delete(void *pv)
  69. #else
  70. void operator delete(void *pv)
  71. #endif
  72. {
  73.     if (pv != NULL)
  74.         {
  75.         MemFree(pv);
  76.         }
  77.     return ;
  78. }
  79.  
  80. #endif  // ifndef COMPILER_MSC
  81.  
  82. //----------------------------------------------------------------------------
  83. //   Description:    Replacement 'new' operator.
  84. //    Parameters:    size        Size of buffer to allocate.
  85. //       Globals:    None modified.
  86. //       Returns:    TRUE if successful.
  87. //----------------------------------------------------------------------------
  88. void CL_OBJECT::operator delete(void *pv)
  89. {
  90.     if (pv)
  91.                 MemFree(pv);
  92. //                ::delete pv;   original code
  93.     return ;
  94. }
  95.  
  96.  
  97. #ifndef COMPILER_MSC
  98.  
  99. //----------------------------------------------------------------------------
  100. //   Description:    Replacement 'new' operator.
  101. //    Parameters:    size        Size of buffer to allocate.
  102. //                                    >= 1.
  103. //       Globals:
  104. //       Returns:    Pointer to allocated buffer or NULL.
  105. //----------------------------------------------------------------------------
  106. #if COMPILE_DLL
  107. void * _EXPORT_ _LOADDS_ operator new(size_t size)
  108. #else
  109. void * operator new(size_t size)
  110. #endif
  111. {
  112.     if (size <= 0)                                // Always allocate at least 1 byte
  113.         size = 1;
  114.     PVOID pv;
  115.     do
  116.         {
  117. #if COMPILE_DEBUG
  118.         pv = MemRealloc_Debug(__FILE__, __LINE__, (SIZET)size, NULL);
  119. #else
  120.         pv = MemRealloc((SIZET)size, NULL);
  121. #endif
  122.         if (_new_handler != NULL && pv == NULL)
  123.             _new_handler();
  124.         }
  125.     while (pv == NULL && _new_handler != NULL);
  126.     return pv;
  127. }
  128.  
  129.  
  130. //----------------------------------------------------------------------------
  131. //   Description:    Replacement 'new' operator.
  132. //    Parameters:    size        Size of buffer to allocate.
  133. //                                    >= 1.
  134. //       Globals:
  135. //       Returns:    Pointer to allocated buffer or NULL.
  136. //----------------------------------------------------------------------------
  137. #if COMPILE_DLL
  138. void * _EXPORT_ _LOADDS_ operator new(size_t size, PCSZ pcszFile, SIZET cLine)
  139. #else
  140. void * operator new(size_t size, PCSZ pcszFile, SIZET cLine)
  141. #endif
  142. {
  143.     if (size <= 0)                                // Always allocate at least 1 byte
  144.         size = 1;
  145.  
  146.     PVOID pv;
  147.     do
  148.         {
  149.         pv = MemRealloc_Debug(pcszFile, cLine, (SIZET)size, NULL);
  150.         if (_new_handler != NULL && pv == NULL)
  151.             _new_handler();
  152.         }
  153.     while (pv == NULL && _new_handler != NULL);
  154.     return pv;
  155. }
  156.  
  157.  
  158. #endif  // ifndef COMPILER_MSC
  159.  
  160. //----------------------------------------------------------------------------
  161. //   Description:    Replacement 'new' operator.
  162. //    Parameters:    size        Size of buffer to allocate.
  163. //                                    >= 1.
  164. //       Globals:
  165. //       Returns:    Pointer to allocated buffer or NULL.
  166. //----------------------------------------------------------------------------
  167. void * _EXPORT_ CL_OBJECT::operator new(size_t size)
  168. {
  169.     if (size <= 0)                                // Always allocate at least 1 byte
  170.         size = 1;
  171.  
  172.     PVOID pv;
  173.     do
  174.         {
  175. #if COMPILE_DEBUG
  176.         pv = MemRealloc_Debug(__FILE__, __LINE__, (SIZET)size, NULL);
  177. #else
  178.         pv = MemRealloc((SIZET)size, NULL);
  179. #endif
  180.         if (_new_handler != NULL && pv == NULL)
  181.             _new_handler();
  182.         }
  183.     while (pv == NULL && _new_handler != NULL);
  184.     return pv;
  185. }
  186.  
  187.  
  188. //----------------------------------------------------------------------------
  189. //   Description:    Replacement 'new' operator.
  190. //    Parameters:    size        Size of buffer to allocate.
  191. //                                    >= 1.
  192. //       Globals:
  193. //       Returns:    Pointer to allocated buffer or NULL.
  194. //----------------------------------------------------------------------------
  195. void * _EXPORT_ CL_OBJECT::operator new(size_t size, PCSZ pcszFile, SIZET cLine)
  196. {
  197.     if (size <= 0)                                // Always allocate at least 1 byte
  198.         size = 1;
  199.  
  200.     PVOID pv;
  201.     do
  202.         {
  203.         pv = MemRealloc_Debug(pcszFile, cLine, (SIZET)size, NULL);
  204.         if (_new_handler != NULL && pv == NULL)
  205.             _new_handler();
  206.         }
  207.     while (pv == NULL && _new_handler != NULL);
  208.     return pv;
  209. }
  210.  
  211.  
  212. //----------------------------------------------------------------------------
  213. //   Description:    Memory allocation failure handler.
  214. //    Parameters:
  215. //       Returns:
  216. //----------------------------------------------------------------------------
  217. #if COMPILE_DLL
  218. pvf _EXPORT_ _LOADDS_ set_new_handler(pvf p)
  219. #else
  220. pvf set_new_handler(pvf p)
  221. #endif
  222. {
  223.      pvf t = _new_handler;
  224.      _new_handler = p;
  225.      return t;
  226. }
  227. //----------------------------------------------------------------------------
  228. //------------------------------- End of File --------------------------------
  229. //----------------------------------------------------------------------------
  230.